package com.dx.as;

import android.app.Application;
import android.content.Context;
import android.util.SparseArray;

import com.dx.as.common.CommonUtils;
import com.dx.as.common.bean.DeviceInfo;
import com.dx.as.common.sys.DeviceUtil;
import com.dx.as.config.preference.Preferences;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import http.HttpCode;

/**
 * Created by jezhee on 2/20/15.
 */
public class JWCache {
    public static final String EXTRA_ONKICKED_OFF_LINE = "onKickedOffline";//对应chat.session.Extras.java 常量
    private static Context context;
    public static Context getContext() {
        return context;
    }
    public static List<Class<?>> activityList = new ArrayList();
    public static void init(Application context){
        JWCache.context = context.getApplicationContext();
        initDevice(context);
        loadHttpCode();
    }

    private static void loadHttpCode(){
        int [] code = getContext().getResources().getIntArray(R.array.http_code);
        String [] code_value = getContext().getResources().getStringArray(R.array.http_code_vaule);
        if (code.length == code_value.length){
            SparseArray sparseArray = new SparseArray(code.length);
            for (int i = 0; i < code.length; i ++){
                sparseArray.put(code[i],code_value[i]);
            }
            HttpCode.setHttpCodeMap(sparseArray);
        }

    }

    public static String getAccount(){
        return CommonUtils.getAccount();
    }

    public static boolean isLogin(){
        return CommonUtils.isLogin();
    }

    public static String getToken(){
        return CommonUtils.getToken() ;
    }

    public static String getNetworkMode(){
        return DeviceUtil.getNetworkType(JWCache.getContext());
    }


    private static void initDevice(Application context){
        if (CommonUtils.getDeviceInfo() == null){
            DeviceInfo deviceInfo = new DeviceInfo();
            //设备号
            String device_no = DeviceUtil.getDeviceId(context);
            //mac地址
            String mac = DeviceUtil.getLocalMacAddressFromWifiInfo(context);
            //android ios
            String os_type = "android";
            String os_version = DeviceUtil.getBuildVersion();
            //设备系统信息
            String hard_ware = DeviceUtil.getPhoneBrand() +" "+ DeviceUtil.getPhoneModel();
            //渠道号
            String channel_id = DeviceUtil.getMetaData(context,"Channel_ID");
            //客户端编号
            String client_id = DeviceUtil.getMetaData(context,"Client_ID");
            //当前版本
            String version = DeviceUtil.getVersionCode(context);
            //运营商
            String hedno = DeviceUtil.getSimOperator(context);
            String token = Preferences.getUserToken();
            deviceInfo.setDevice_no(device_no);
            deviceInfo.setMac(mac);
            deviceInfo.setOs_type(os_type);
            deviceInfo.setHard_ware(hard_ware);
            deviceInfo.setChannel_id(channel_id);
            deviceInfo.setClient_id(client_id);
            deviceInfo.setVersion(version);
            deviceInfo.setHedno(hedno);
            deviceInfo.setToken(token);
            deviceInfo.setOs_version(os_version);
            CommonUtils.setDeviceInfo(deviceInfo);
        }
    }
}
